var HardSourceWebpackPlugin = require('hard-source-webpack-plugin');
const AddAssetHtmlPlugin = require('add-asset-html-webpack-plugin');
const CracoLessPlugin = require("craco-less");
const webpack = require("webpack");
const path = require("path");

// to work with dll, you must create dll manually with 'npm run build:dll' first
const dllEnabled = false;

module.exports = {
  webpack: {
    plugins: {
      add: dllEnabled && process.env.NODE_ENV == "production" ? [
         new webpack.DllReferencePlugin({
          context: process.cwd(),
          manifest: require('./public/vendor/vendor-manifest.json')
        }),
        new AddAssetHtmlPlugin({
          // dll文件位置
          filepath: path.resolve(__dirname, './public/vendor/*.js'),
          // dll 引用路径
          publicPath: './vender',
          // dll最终输出的目录
          outputPath: './vendor'
        }),
      ] : [
        new HardSourceWebpackPlugin()
      ],
    }
  },
  plugins: [
    {
      plugin: CracoLessPlugin,
      options: {
        lessLoaderOptions: {
          lessOptions: {
            // modifyVars: { '@primary-color': '#1DA57A' },
            javascriptEnabled: true,
          },
        },
      },
    },
    {
      plugin: CracoLessPlugin,
      options: {
        modifyLessRule: function (lessRule, _context) {
          lessRule.test = /\.(module)\.(less)$/;
          lessRule.exclude = /node_modules/;

          return lessRule;
        },
        cssLoaderOptions: {
          modules: {
            localIdentName: "[local]_[hash:base64:5]",
          },
        },
      },
    },
  ],
};
